package com.tyf.common.util;

import com.tyf.common.enums.ConfirmFlagEnum;
import com.tyf.common.log.LogUtils;
import com.tyf.common.vo.SystemUser;

import java.util.*;

/**
 * 
 * @ClassName: BizUtils<br>
 * @Description: 业务相关工具类<br>
 * @author liutao<br>
 * @date 2016年10月22日下午6:19:44<br>
 *
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class BizUtils {
	/**
	 * 本地调试绕过feignAuth认证
	 */
	public static final String FEIGNAUTH_TOKEN = "local-feignAuth-token";
	
	private BizUtils() {}
	
	/**
	 * 获取当前登录用户code
	 * @return
	 */
	public static String getCurrentUserCode() {
		SystemUser user = ThreadLocals.getCurrentUser();
		if(user != null){
			return user.getUserCode();
		}
		return "";
    }
	
	/**
	 * 获取一个默认用户admin
	 * @return
	 */
	public static SystemUser getDefaultUser() {
		SystemUser user = new SystemUser();
		user.setId(new Long(1));
		user.setUserCode("admin");
		user.setUserName("自动");
		//本地调试绕过feignAuth认证，不产生token，不能删除
		user.setEmail(FEIGNAUTH_TOKEN);
		user.setAdmin(true);
		return user;
	}

	public static boolean isSuperAdmin(SystemUser systemUser){
		return (systemUser!=null && systemUser.getAdmin());
	}
	
	/**
	 * 设置单个实体的默认属性
	 * 
	 * @param entity
	 * @param reqType
	 *            0:新增，1：修改
	 * @param date
	 */
	public static void setEntityDefaultField(Object entity, int reqType, SystemUser systemUser, Date date) {
		if (reqType == 0) {
			ReflectUtils.setFieldValue(entity, "createTime", date, true);
			ReflectUtils.setFieldValue(entity, "creator", systemUser.getUserName(), true);
		}
		ReflectUtils.setFieldValue(entity, "modifyTime", date, true);
		ReflectUtils.setFieldValue(entity, "modifier", systemUser.getUserName(), true);
	}
	
	/**
	 * 设置单据审核默认属性
	 * @param entity
	 * @param billStatus
	 * @param systemUser
	 */
	public static void setBillAuditDefaultField(Object entity, String billStatus, SystemUser systemUser){
		Date date = new Date();
		String userName = systemUser.getUserName();
		ReflectUtils.setFieldValue(entity, "modifyTime", date, true);
		ReflectUtils.setFieldValue(entity, "modifier", userName, true);
		ReflectUtils.setFieldValue(entity, "auditTime", date);
		ReflectUtils.setFieldValue(entity, "auditor", userName);
		ReflectUtils.setFieldValue(entity, "billStatus", billStatus);
		
	}
	
	/**
	 * 设置单表审核默认属性
	 * @param entity
	 * @param systemUser
	 */
	public static void setEntityAuditDefaultField(Object entity, SystemUser systemUser){
		Date date = new Date();
		String userName = systemUser.getUserName();
		ReflectUtils.setFieldValue(entity, "modifyTime", date, true);
		ReflectUtils.setFieldValue(entity, "modifier", userName, true);
		ReflectUtils.setFieldValue(entity, "confirmFlag", ConfirmFlagEnum.CONFIRMED.value());
		
	}
	
    /**
	 * 获取clazz <br>
	 * {@code @eg1}<br>
	 * {@code com.tyf.{projectName}.dao.integration.entity.ItgApplication}<br>
	 * {@code 与  com.tyf.{projectName}.dao.integration.vo.other.ApplicationOVO}<br>
	 * {@code 前端请求为bl-{projectName}-web/itg_application/findVO.json?VO=other.ApplicationOVO}<br>
	 * {@code @eg2}<br>
	 * {@code com.tyf.{projectName}.dao.integration.entity.ItgProject}<br>
	 * {@code 与  com.tyf.{projectName}.dao.integration.vo.ProjectVO}<br>
	 * {@code 前端请求为bl-{projectName}-web/itg_project/findVO.json?VO=ProjectVO}
	 * @param vo 为相对于实体的vo包下面的-包路径名<br>
	 * @return vo的class
	 */
	public static Class<?> getVOClazz(Class<?> entityClass, String vo){
		Class<?> voClazz = null;
		
		// 先找相对的vo目录，再找以vo结束的目录，再找以dto结束的目录
		String orgPackageName = entityClass.getPackage().getName();
		String[] packageNames = new String[3];
		packageNames[0] = orgPackageName.replace(".domain", ".vo");
		packageNames[1] = orgPackageName.replaceAll("\\.domain.*", ".vo");		// \\转义特殊字符
		packageNames[2] = orgPackageName.replaceAll("\\.domain.*", ".dto");
		voClazz = getVOClazzForNames(packageNames, vo);
		if(voClazz == null){
			throw new IllegalArgumentException("path " + Arrays.toString(packageNames) +" did not found " + vo +"!");
		}
		
		return voClazz;
	}
	
	private static Class<?> getVOClazzForNames(String[] packageNames, String vo){
		Class<?> voClazz = null;
		
		for(String packageName : packageNames){
			try {
				voClazz =  Class.forName(packageName + "." + vo);
				break;
			} catch (ClassNotFoundException e) {
				LogUtils.logDebug("path [" + packageName +"] did not found " + vo +"!");
			}
		}
		
		return voClazz;
	}
}
